//
//  NSObject+LLUtil.m
//  lrlzapp
//
//  Created by dl on 16/2/25.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "NSObject+LL.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSObject (LL)

- (BOOL)isNullStr {
    if (self == nil || self == NULL) {
        return YES;
    }
    if ([self isKindOfClass:[NSNull class]]) {
        return YES;
    }

    NSString *tmpStr = [NSString stringWithFormat:@"%@", self];
    return [tmpStr length] == 0
        || [tmpStr isEqualToString:@"(null)"]
        || [tmpStr isEqualToString:@"<null>"]
        || [tmpStr isEqualToString:@"null"];
}

@end


@implementation NSString (LL)

- (BOOL)isTelephone {
    NSString *regex = @"^（(13)|(14)|(15)|(17)|(18))\\d{9}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}

- (BOOL)isCheckCode {
    NSString *regex = @"\\d{4}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}

- (NSString *)urlEncodedString {
    NSString *encodedString = (NSString *)
            CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                    (__bridge CFStringRef) self,
                    (CFStringRef) @"!$&'()*+,-./:;=?@_~%#[]",
                    NULL,
                    kCFStringEncodingUTF8));
    return encodedString;
}

- (NSString *)md5Hash {

    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];

    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5([data bytes], [data length], result);

    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
    ];
}

- (NSString *)sha1 {
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, data.length, digest);

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }

    return output;
}

// 从URL的param字符串中，提取key对应的value
- (NSString *)getValueFromUrlParamStrWithKey:(NSString *)key {
    NSError *error;
    NSString *regTags = [[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)", key];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *matches = [regex matchesInString:self
                                      options:0
                                        range:NSMakeRange(0, [self length])];
    for (NSTextCheckingResult *match in matches) {
        NSString *tagValue = [self substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
        return tagValue;
    }
    return nil;
}

- (NSString *)humanSalesCnt {
    if ([self intValue] >= 10000) {
        CGFloat cnt = [self intValue] / 10000.f;
        return [NSString stringWithFormat:@"%.1f万", cnt];
    } else {
        return [NSString stringWithFormat:@"%d", [self intValue]];
    }
}

- (NSString *)stringByDecodingURLFormat {
    NSString *result = [(NSString *) self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}

@end